#include "scene/wsyperspectivecamera.h"
#include "scene/getperspectivematrix.h"
#include <QMatrix4x4>

/** 获取摄像机的透视矩阵 */
GLfloat *WSYPerspectiveCamera::getCameraMatrix() {
    //delete this->cameraMatrix;

    Math3D::directionNew(position, lookAt, cameraLookAtDirectionNew);
    Math3D::directionNewAndNormalize(position, lookAt, nCameraLookAtDirectionNew);
    Math3D::crossVectorsNew(cameraLookAtDirectionNew, cameraUpNew, xDirectionNew);
    Math3D::normalizeWithChanging(xDirectionNew);

    Math3D::crossVectorsNew(xDirectionNew, cameraLookAtDirectionNew, yDirectionNew);
    Math3D::normalizeWithChanging(yDirectionNew);

    //当前的摄像机的位置
    double CX = this->position->getX();
    double CY = this->position->getY();
    double CZ = this->position->getZ();

    //摄像机到焦点的单位向量
    double A_c = nCameraLookAtDirectionNew[0];
    double B_c = nCameraLookAtDirectionNew[1];
    double C_c = nCameraLookAtDirectionNew[2];

    //摄像机X方向的单位向量
    double A_lx = xDirectionNew[0];
    double B_lx = xDirectionNew[1];
    double C_lx = xDirectionNew[2];

    //摄像机Y方向的单位向量
    double A_ly = yDirectionNew[0];
    double B_ly = yDirectionNew[1];
    double C_ly = yDirectionNew[2];

   //GLfloat *positiveMatrix = new GLfloat[16];

   Matrix3D::appendValuesDouble(positiveMatrixNew,
       A_lx, B_lx, C_lx, 0,
       A_ly, B_ly, C_ly, 0,
       A_c, B_c, C_c, 0,
       CX, CY, CZ, 1);

   //Matrix3D::printSingleMatrix("positive", positiveMatrix);

   Matrix3D::inverseMatrixNew(positiveMatrixNew, negativeMatrixNew);
   //GLfloat *negativeMatrix = Matrix3D::inverseMatrix(positiveMatrix);

//   QMap<QString, GLfloat*> *perMatrix =
//           GetPerspectiveMatrix(this->fov, this->aspect, this->near1, this->far1);

   GetPerspectiveCameraMatrixNew(fov, aspect, near1, far1, middleMatrixNew, perspectiveMatrixNew);

   //GLfloat *middleMatrix = perMatrix->take("middleMatrix");
   //GLfloat *perspectiveMatrix = perMatrix->take("perspectiveMatrix");

   //QList<GLfloat *> *matrixArray = new QList<GLfloat *>();
   //matrixArray->push_back(negativeMatrix);
   //matrixArray->push_back(middleMatrix);
   //matrixArray->push_back(perspectiveMatrix);

   //GLfloat *returnMatrix = Matrix3D::multiplyVertices(matrixArray);

   Matrix3D::multiplyTwoMatricesNew(negativeMatrixNew, middleMatrixNew, negativeMiddleMatrixNew);
   Matrix3D::multiplyTwoMatricesNew(negativeMiddleMatrixNew, perspectiveMatrixNew, cameraMatrix);

   //delete matrixArray;
   //delete negativeMatrix; delete middleMatrix; delete perspectiveMatrix;

   //delete perMatrix;
   //delete[] positiveMatrix;

   //this->cameraMatrix = returnMatrix;
   return cameraMatrix;
}

QMatrix4x4 *WSYPerspectiveCamera::getCameraMatrix2() {
    QMatrix4x4 *newMatrix = new QMatrix4x4();
    newMatrix->perspective(60.0f, 4.0f/3.0f, 0.1f, 100.0f);


    QVector3D *eye = new QVector3D(position->getX(), position->getY(), position->getZ());
    QVector3D *center = new QVector3D(lookAt->getX(), lookAt->getY(), lookAt->getZ());
    QVector3D *up = new QVector3D(cameraUp->getDeltaX(), cameraUp->getDeltaY(), cameraUp->getDeltaZ());

    //QVector3D *center = new QVector3D()

    newMatrix->lookAt(*eye, *center, *up);
    return newMatrix;
}
